package com.example.flashApp.usage

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.flashApp.databinding.FragmentUsageStatsBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class UsageStatsFragment : Fragment() {

    private var _binding: FragmentUsageStatsBinding? = null
    private val binding get() = _binding!!
    private val viewModel: UsageViewModel by viewModels()
    private lateinit var adapter: UsageStatsAdapter

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentUsageStatsBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        
        setupRecyclerView()
        observeViewModel()
    }

    private fun setupRecyclerView() {
        adapter = UsageStatsAdapter()
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = this@UsageStatsFragment.adapter
            setHasFixedSize(true)
        }
    }

    private fun observeViewModel() {
        viewModel.usageStats.observe(viewLifecycleOwner) { stats ->
            adapter.submitList(stats)
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}
